﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace VisionApplication
{
    //written by Pascal Schotman
    class PeriodicNoise : VisionAlgorithm
    {

        public PeriodicNoise(String name, string[] required)
            : base(name, required)
        {
        }

        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage, Object obj)
        {
            Bitmap returnImage = new Bitmap(sourceImage);

            //copy to FastPixelAccess class for fast access
            FastPixelAccess retImg = new FastPixelAccess(returnImage);

            //lock images for use
            retImg.Lock();

            //int x, y = 0;

            //  width/rows to define gap space
            int gap = ((retImg.Width + retImg.Height) / settings.Rows);

            int x, y, temp = 0;

            for (int i = 0; i < settings.Rows; i++)
            {
                y = 0;
                for (x = gap * i; x < retImg.Width; x++)
                {
                    //if i == 0, lijn dunner (/2)
                    if (i == 0)
                    {
                        for (int thick = 0; thick < settings.Width / 2 + 1; thick++)
                        {
                            if (y < retImg.Height && x < retImg.Width - thick)
                            {
                                temp = x + thick;
                                retImg.SetPixel(temp, y, Color.White);
                            }
                        }
                    }
                    else
                    {
                        for (int thick = 0; thick < settings.Width; thick++)
                        {
                            if (y < retImg.Height && x < retImg.Width - thick)
                            {
                                temp = x + thick;
                                retImg.SetPixel(temp, y, Color.White);
                            }
                        }
                    }
                    y++;
                }
                x = 0;
                for (y = gap * i; y < retImg.Height; y++)
                {
                    // if i == 0 , lijn dunner (/2)
                    if (i == 0)
                    {
                        for (int thick = 0; thick < settings.Width / 2 + 1; thick++)
                        {
                            if (x < retImg.Width && y < retImg.Height - thick)
                            {
                                temp = y + thick;
                                retImg.SetPixel(x, temp, Color.White);
                            }
                        }
                    }
                    else
                    {
                        for (int thick = 0; thick < settings.Width; thick++)
                        {
                            if (x < retImg.Width && y < retImg.Height - thick)
                            {
                                temp = y + thick;
                                retImg.SetPixel(x, temp, Color.White);
                            }
                        }
                    }
                    x++;
                }
            }

            //unlock and return image for showing
            retImg.Unlock(true);
            this.Image = returnImage;
            return returnImage;
        }
    }
}